package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
//		if (cachedContacts == null) {
			rebuildContactCache();
//		}
		return cachedContacts;
	}

	private void rebuildContactCache() {
		cachedContacts = new SortedListOf<ContactData>();

		manager.navigateTo().mainPage();
		List<WebElement> rows = driver.findElements(By.name("entry"));
		for (WebElement row : rows) {
			List<WebElement> cells = row.findElements(By.tagName("td"));
			String firstName = cells.get(2).getText();
			String lastName = cells.get(1).getText();
			cachedContacts.add(new ContactData()
										.withFirstName(firstName)
										.withLastName(lastName));
			}
	}

	public ContactHelper createContact(ContactData contact, boolean CREATION) {
		manager.navigateTo().addNewContactPage();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToMainPage();
		rebuildContactCache();
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().mainPage();
		selectEditContactByIndex(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToMainPage();
		rebuildContactCache();
		return this;
	}

	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		selectEditContactByIndex(index);
		submitContactDeletion();
		returnToMainPage();
		rebuildContactCache();
		return this;
	}
	
	public ContactHelper deleteContactFromDetails(int index) {
		manager.navigateTo().mainPage();
		openContactDetais(index);
		initContactDetaisModification();
		submitContactDeletion();
		returnToMainPage();
		rebuildContactCache();
		return this;
	}

//----------------------------------------------

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getPhoneHome());
		type(By.name("mobile"), contact.getPhoneMobile());
		type(By.name("work"), contact.getPhoneWork());
		type(By.name("email"), contact.getEmail1());
		type(By.name("email2"), contact.getEmail2());
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getByear());
		type(By.name("address2"), contact.getSecAddress());
		type(By.name("phone2"), contact.getSecHome());
/*		if (formType == CREATION) {
			//selectByText(By.name("new_group"), "group 1");			
		} else {
			if (driver.findElements(By.name("new_group")).size() != 0) {
				throw new Error ("Group selector exists in contact modification form");
			}
		}
*/
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
		}

	public ContactHelper returnToMainPage() {
		click(By.linkText("home page"));
		return this;
		}

	public ContactHelper selectEditContactByIndex(int index) {
		click(By.xpath("(//img[@alt='Edit'])[" + (index + 1) + "]"));
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("(//input[11][@name='update'])"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper openContactDetais(int index) {
		click(By.xpath("(//img[@alt='Details'])[" + (index + 1) + "]"));
		return this;
	}

	public ContactHelper initContactDetaisModification() {
		click(By.name("modifiy"));
		return this;
	}

	private void submitContactDeletion() {
		click(By.xpath("(//input[2][@name='update'])"));
		cachedContacts = null;
	}


	public ContactHelper addingContactToGroup() {
		selectByText(By.name("to_group"), "group z");
		click(By.name("add"));
		return this;
	}

	public ContactHelper returnToContactListForGroup() {
		click(By.linkText("group page \"group z\""));
		return this;
	}
	
	public ContactHelper openContactsListForGroup() {
		selectByText(By.name("group"), "group z");
		return this;
	}

	public ContactHelper selectContactZ() {
		click(By.xpath("//input[@alt='Select (First name Z Last name Z)']"));
		return this;
	}

	public ContactHelper submitRemovingContactFromGroup() {
		click(By.name("remove"));
		return this;
	}

}